package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.common.bcrypt.PasswordConfig;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.common.util.JwtUtils;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.dtos.WmUserLoginDto;
import com.heima.wemedia.entity.WmUser;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.awt.geom.QuadCurve2D;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-27
 */
@Slf4j
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {


    @Autowired
    private WmUserMapper  wmUserMapper;

    /**
     * 保存自媒体用户
     * @param dto
     * @return
     */
    @Override
    public Integer saveWmUser(WmUserDto dto) {

//        根据ApUserId 查询自媒体用户
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getApUserId,dto.getApUserId());
        WmUser wmUser = getOne(queryWrapper);
//        新增自媒体用户
        if(wmUser == null){
            wmUser = new WmUser();
            wmUser.setApUserId(dto.getApUserId());
        }
        wmUser.setName(dto.getName());
        wmUser.setPassword(dto.getPassword());
        wmUser.setStatus(dto.getStatus());
        wmUser.setPhone(dto.getPhone());

        boolean b = this.saveOrUpdate(wmUser);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR);
        }

        return wmUser.getId();
    }

    @Autowired
    private BCryptPasswordEncoder encoder;


    /**
     * 登录
     * @param dto
     * @return
     */
    @Override
    public Map<String, Object> login(WmUserLoginDto dto) {
        // 根据name 查询用户
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmUser::getName,dto.getName());

        WmUser wmUser = getOne(queryWrapper);
        // 判断是否为空
        if (wmUser == null) {
            log.error("用户登录失败,没用该用户");
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 比较密码是否正确
        String password = dto.getPassword();
        // 第一个是传递的密码 .第二个是数据库查出来的密码
        boolean b = encoder.matches(dto.getPassword(), wmUser.getPassword());

        // 判断是否正确
        if (!b){
            // 登录失败,密码错误
            log.error("登录失败,密码错误");
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 保存token
        wmUser.setPassword(null);

        String token = JwtUtils.generateTokenExpireInMinutes(wmUser.getId(), 60);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("token",token);
        hashMap.put("user",wmUser);

        return hashMap;
    }



}
